/* Analysis of Transfer and electoral support for Japanese elections */ 
/* A Tournament Theory of Pork Barrel Politics: The Case of Japan
Amy Catalinac Bruce Bueno de Mesquita Alastair Smith */


/* The data was constructed in "OrganizeJapanDataMay2018.do"  and saved as tempMay2018.dta */
 
clear
set matsize 11000



use "JapanTournamentData",clear

 

/* HOR district are redistricted in 1994-- hence the label prior to 1994 and label post 1994 dont correpsond to the same district */ 
gen double  HOR=  hor_electoral_district  if year <1994
replace HOR=  hor_electoral_district*100  if year >1994
gen logsdVotePop=log(sdVotePop)
/* creature global lists to save on typing */ 
#delimit;
 global controls  " mun_ceif needy_pc  primary_pc lnpop  logincome_pc ";

global o2best "bestLDP_VshareVP bestLDPp_VshareVP bestSeniorLDP_VshareVP bestnonLDP_VshareVP  
bestloseLDP_VshareVP";
global o2sum "sumLDP_VshareVP sumLDPp_VshareVP sumSeniorLDP_VshareVP sumnonLDP_VshareVP  
sumloseLDP_VshareVP logngaid_pc";
#delimit;


#delimit;
global labDIS "HI "Population Index. HI" DISmun_ceif "District: Fiscal Strength" num "No. Municipalities" DisWinLDPVS "$\text{Winning LDP VS}_{m,t}$"
DisLDP "District: LDP winner" DISmun_needy_pc "District: Needy Pop."
 DISprimary_pc "District: Agriculture" DIS_urban "District: Urban" DisLogPop "District: Log(Pop)" 
 DisLogIncome "District: Log(Income)" malaportion "Malaportionment" DisPopDensity "District: Pop.Density" "; 

#delimit;
global lab " lnpop  "Log(Population)" Llogngaid_pc "Log(Transfers(t-1))" logngaid_pc "Log(Transfers(t))" F1logngaid_pc "Log(Transfers(t+1))" mun_ceif "Fiscal Strength" needy_pc "Needy Population" primary_pc "Agriculture"  logincome_pc  "Log(Income)" bestLDP_VshareVP "Best LDP VS"  
bestLDPp_VshareVP "Best LDP+ VS" bestloseLDP_VshareVP "Best Losing LDP VS"
 bestLDP_VshareVP "Best LDP VS"  bestSeniorLDP_VshareVP "Best Senior LDP VS" bestnonLDP_VshareVP "Best Non-LDP VS" 
 sumLDPp_VshareVP "Sum LDP+ VS" sumloseLDP_VshareVP "Sum Losing LDP VS"
 sumLDP_VshareVP "Sum LDP VS"  sumSeniorLDP_VshareVP "Sum Senior LDP VS" sumnonLDP_VshareVP "Sum Non-LDP VS" 
 rbestLDPp_VshareVP "Rank Best LDP+ VS"  rsumLDPp_VshareVP "Rank Sum LDP+ VS"
 rbestPropLDP "Rank Best LDP PS"  rsumPropLDP "Rank Sum LDP PS" 
 rF1logngaid_pc "$\text{Rank Transfers}_{t+1}$" logngaid_pc "$\text{Transfer}_{m,t-1}$" rbestLDP_VshareVP "Rank(Best LDP VS)" mun_population_density "Pop.Density" 
 rsumLDP_VshareVP "Rank(Sum LDP VS)"
 rbestSeniorLDP_VshareVP "Rank(Best Senior LDP VS)" rbestloseLDP_VshareVP "Rank(Best Losing LDP VS)"  rbestnonLDP_VshareVP "Rank(Best Non-LDP VS)" 
  rsumSeniorLDP_VshareVP "Rank(Sum Senior LDP VS)" rsumloseLDP_VshareVP "Rank(Sum Losing LDP VS)"  rsumnonLDP_VshareVP "Rank(Sum Non-LDP VS)" 
 HILlogngaid "HI*Log(Transfers(t-1))" ";
#delimit;
global lab2 " LFtransfer "Log(Transfer) after 1993" dHI "$\Delta$ HI" 
dbestLDP_VshareVP "$\Delta \text{Best LDP VS}$"
 dHIdbestLDP "$\Delta \text{Best LDP VS} *\Delta$ HI" 
 drbestLDP_VshareVP "$\Delta \text{Rank Best LDP VS}$"
  dHIdrbestLDP "$\Delta \text{Rank Best LDP VS} *\Delta$ HI"
  dsumLDP_VshareVP "$\Delta \text{Sum LDP VS}$"
 dHIdsumLDP "$\Delta \text{Sum LDP VS} *\Delta$ HI" 
 drsumLDP_VshareVP "$\Delta \text{Rank Sum LDP VS}$"
  dHIdrsumLDP "$\Delta \text{Rank Sum LDP VS} * \Delta$ HI"
  rbestLDP_VshareVP "$\text{Rank Best LDP VS}$"
  rsumLDP_VshareVP "$\text{Rank Sum LDP VS}$"";
  
#delimit;
global deltaIV " dmun_ceif "$\Delta$ Fiscal Strength" 
dneedy_pc "$\Delta$ Needy Population"  
dprimary_pc "$\Delta$ Agriculture"  
dlnpop "$\Delta$ Ln(Population)"
 dlogincome_pc "$\Delta$ Ln(Income)" ";  
 

 
 
/************** Graphical Approach *****************/
 #delimit;

twoway (qfitci Transfers rbestLDP_VshareVP if year<=2000), ytitle(Transfers per capita (t+1)) 
xtitle(rank(Best LDP VS)) 
/*title(Per Capita Transfers and Rank of Best Support for Winning LDP Candidate, size(medium))*/
 legend(off) name(basicTransferRank,replace);
graph export "basicTransferRank.pdf", name(basicTransferRank) replace; 

#delimit;
twoway (qfitci Transfers rbestLDP_VshareVP if year<=2000) 
(scatter Transfers rbestLDP_VshareVP if year<=2000 & Transfers<.1,
 msymbol(circle) msize(vtiny) jitter(1)), ytitle(Transfers pc (t+1)) 
xtitle(rank(Best LDP Vote Share)) 
/*title(Per Capita Transfers and Rank of Best Support for Winning LDP Candidate, size(medium))*/
 legend(off) name(basicTransferRankwScatter,replace);
graph export "basicTransferRankwScatter.pdf", name(basicTransferRankwScatter) replace; 

 #delimit;

twoway (qfitci F1logngaid_pc rbestLDP_VshareVP if year<=2000) 
(scatter F1logngaid_pc rbestLDP_VshareVP if year<=2000 & Transfers<.2,
 msymbol(circle) msize(vtiny) jitter(1)), ytitle(Log(Transfers pc (t+1))) 
xtitle(rank(Best LDP VS)) 
/*title(Per Capita Transfers and Rank of Best Support for Winning LDP Candidate, size(medium))*/
 legend(off) name(basicTransferRankwScatter,replace);
graph export "basicLogTransferRankwScatter.pdf", name(basicTransferRankwScatter) replace; 

/* there are numerous other graphs - see Generate_Order_stats_for_Japan3.do */






	/*************** Graphs at the District level *********************/
#delimit;
 capture gen logFDismoneypc=log(FDismoneypc);
twoway (qfitci FDismoneypc  HI if last==1 & year<=2000 &HI<.4&ey==1)
(kdensity HI  if year<=2001& last==1&HI<.4&ey==1, yaxis(2)) 
, ytitle("District: Transfers per capita (t+1)") ytitle("Density of HI", axis(2))  
xtitle("HI Index")
 /*title("District Level pc Transfers and Herfindahl Index of Voting Population", size(medium))*/
 legend(off) name(DisMoneyHI,replace);
graph export "DisMoneyHI.pdf", name(DisMoneyHI) replace;



#delimit;
twoway (qfitci FDismoneypc  HI if last==1 & year<=2000 &HI<.4&ey==1 & FDismoneypc<.2)
(scatter FDismoneypc  HI if last==1 & year<=2000 &HI<.4&ey==1& FDismoneypc<.2,msize(vtiny) msymbol(circle))
(kdensity HI  if year<=2001& last==1&HI<.4&ey==1, yaxis(2)) 
, ytitle(District Transfers pc) ytitle("Density of HI", axis(2))  
xtitle("Voting Population Concentration, HI")
 /*title("District Level pc Transfers and Herfindahl Index of Voting Population", size(medium))*/
 legend(off) name(DisMoneyHI,replace);
graph export "DisMoneyHIwScatter.pdf", name(DisMoneyHI) replace;









/* Distribution of HI before and after reform */ 
/**************************************/
/***** Figure A1 *****/
/**************************************/
#delimit;
twoway (kdensity HI  if year<=2001& last==1&HI<.6&ey==1 &year < 1995, yaxis(1) lcolor(black) lwidth(thick) lpattern(solid) ) 
(kdensity HI  if year<=2001& last==1&HI<.6&ey==1 &year > 1995, yaxis(1) lcolor(red) lwidth(thick) lpattern(dash))
,  ytitle("Density of HI")  
xtitle("Voting Population Concentration, HI")
text(7 .07  "Before", color(black) place(e)) text(.65 .5 "After", color(red) place(e))
 /*title("Herfindahl Index of Voting Population: Before and After Reform", size(medium))*/
 legend(off) name(HIdistribution,replace);
graph export "HIdistribution.pdf", name(HIdistribution) replace;



#delimit;
twoway (qfitci DisWinLDPVS  HI if first==1 &ey==1 &year < 2001 & HI<.6)
, ytitle(Proportion of Votes for Winning LDP Candidates)   
xtitle("HI Index") 
/*title("Votes for Winning LDP Candidates and Population Concentration", size(medium))*/
 legend(off) name(LDPwinnerVotesVsHI,replace);
graph export "LDPwinnerVotesVsHI.pdf", name(LDPwinnerVotesVsHI) replace;

#delimit;
twoway (qfitci DisWinLDPVS  HI if first==1 &ey==1 &year < 2001 & HI<.6 )
(scatter DisWinLDPVS  HI if first==1 &ey==1 &year < 2001 & HI<.6, msymbol(circle) msize(tiny))
, ytitle(Proportion of Votes for Winning LDP Candidates)   
xtitle("Concentration Index, HI") 
/*title("Votes for Winning LDP Candidates and Population Concentration", size(medium))*/
 legend(off) name(LDPwinnerVotesVsHIwScatter,replace);
graph export "LDPwinnerVotesVsHIwScatter.pdf", name(LDPwinnerVotesVsHIwScatter) replace;


#delimit;
twoway (qfitci DisWinLDPVS  HI if first==1 &ey==1 &year < 2001 & HI<.4 &DisWinLDPVS>0)
(scatter DisWinLDPVS  HI if first==1 &ey==1 &year < 2001 & HI<.4&DisWinLDPVS>0, msymbol(circle) msize(tiny))
, ytitle(Proportion of Votes for Winning LDP Candidates)   
xtitle("Voting Population Concentration, HI") 
/*title("Votes for Winning LDP Candidates and Population Concentration", size(medium))*/
 legend(off) name(LDPwinnerVotesVsHIwScatterNoZero,replace);
graph export "LDPwinnerVotesVsHIwScatterNoZero.pdf", name(LDPwinnerVotesVsHIwScatterNoZero) replace;


/**************** Summary Statistics *****************/
#delimit;
/****************************** Table A1 **************************/
 sutex2 logngaid_pc bestLDP_VshareVP sumLDP_VshareVP mun_ceif needy_pc  primary_pc lnpop logincome_pc mun_population_density if ey==1 &year<=2000;

 
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
/*************** District level regression ****************/ 
/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/
 capture gen logFDismoneypc=log(FDismoneypc);
#delimit ;
global districtLab "HI "HI" num "No. Municipalities"  DisLDPVS "LDP VS" 
DISmun_needy_pc "Needy Population" DISprimary_pc "Agriculture" 
DIS_urban "Extent Urban" DisLogPop "log(Population)" logNum "log(Nunmber of Municipalities)"
DisLogIncome "log(Income pc)" sdVoteP "std dev(VotePopulation)" DISmun_ceif " District: Fiscal Stength"";

 #delimit;
 global xvar2 "logNum DisWinLDPVS DISmun_ceif DISmun_needy_pc DISprimary_pc  DisLogPop  
 DisLogIncome malaportion DisPopDensity i.year";   

#delimit ;
reg logFDismoneypc HI $xvar2 if last==1 & year<=2000, vce(cluster HOR);
eststo d1a;  
 estadd local yearFE "Y";
estadd local disFE "N";
#delimit ;
reg logFDismoneypc  $xvar2 if last==1 & year<=2000, vce(cluster HOR);
eststo d1anohi;  
 estadd local yearFE "Y";
estadd local disFE "N";

#delimit ;
reg logFDismoneypc HI  $xvar2 if last==1   &year<1994 , vce(cluster HOR);
eststo d2a;
estadd local yearFE "Y";
estadd local disFE "N";
#delimit ;
reg logFDismoneypc   $xvar2 if last==1   &year<1994 , vce(cluster HOR);
eststo d2anohi;
estadd local yearFE "Y";
estadd local disFE "N";


#delimit ;
xtreg logFDismoneypc HI  $xvar2  if last==1 &year<=2000 ,fe i(HOR) vce(cluster HOR);
eststo d1fea;  
estadd local yearFE "Y";
estadd local disFE "Y";

#delimit ;
xtreg logFDismoneypc HI  $xvar2  if last==1 &year<1994  ,
 fe i(HOR) vce(cluster HOR);
eststo d2fea;  
estadd local yearFE "Y";
estadd local disFE "Y";
 #delimit;	



/**********************************************************/ 
/***************** Table 3 ************************/
/**********************************************************/
esttab d1a d1fea d2a d2fea using "Table3.tex",replace  ar2 r2 se 
   title("Log of district level per capita transfers after an election are regressed on 
   support for the LDP (Models 1-4) and 
   the concentration of voting population (\$HI_{d,t}\$  in HOR elections. Districts comprised of 
   municipalities of asymmetric sizes get more transfers. Models 1 and 2 consider the seven election (1980-2000). 
   Models 3 and 4 consider elections prior to refer (pre-1994). \label{tab:dis}" )
   stats( N yearFE disFE r2,
	labels("Observations" "Year FE" "District FE" "R-squared")fmt(0 0 0 2))
 varlabel($labDIS $districtLab) order(HI  DisWinLDPVS DISmun_ceif DISmun_needy_pc DISprimary_pc  DisLogPop  
 DisLogIncome  DisPopDensity logNum malaportion)
 drop(*.year  _cons) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers);  
 
 
 /* lagged transfers */
 #delimit;
 global xvar "logNum DisWinLDPVS DISmun_ceif DISmun_needy_pc DISprimary_pc  DisLogPop  DisLogIncome malaportion DisPopDensity i.year"; 
#delimit ;
reg logFDismoneypc HI logngaid_pc $xvar if last==1 & year<=2000, vce(cluster HOR);
eststo d1b;  
 estadd local yearFE "Y";
estadd local disFE "N";
#delimit ;
reg logFDismoneypc HI logngaid_pc $xvar 
 if last==1   &year<1994 , vce(cluster HOR);
eststo d2b;
estadd local yearFE "Y";
estadd local disFE "N";

#delimit ;
xtreg logFDismoneypc HI logngaid_pc $xvar  if last==1 &year<=2000 ,fe i(HOR) vce(cluster HOR);
eststo d1bfe;  
estadd local yearFE "Y";
estadd local disFE "Y";

#delimit ;
xtreg logFDismoneypc HI logngaid_pc $xvar  if last==1 &year<1994  ,
 fe i(HOR) vce(cluster HOR);
eststo d2bfe;  
estadd local yearFE "Y";
estadd local disFE "Y";
 #delimit;	
/**********************************************************/
/********************Table A6 *********************/
/**********************************************************/
esttab d1b d1bfe d2b d2bfe using "tableA6.tex",replace  ar2 r2 se 
   title("Log(Transfers pc) to districts are regressed on support for the LDP (Models 1-4) and 
   the concentration of voting population (\$HI_{d,t}\$  in HOR elections. Districts comprised of 
   municipalities of asymmetric sizes get more transfers. Models 1 and 2 consider the seven election (1980-2000). 
   Models 3 and 4 consider elections prior to refer (pre-1994)." )
   stats( N yearFE disFE r2,
	labels("Observations" "Year FE" "District FE" "R-squared")fmt(0 0 0 2))
 varlabel($labDIS $districtLab) order(HI logngaid_pc logNum DisWinLDPVS DISmun_ceif  DISmun_needy_pc DISprimary_pc  DisLogPop  DisLogIncome  DisPopDensity malaportion)
 drop(*.year  _cons) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers); 
 
 /* Use SD of muni size instead of HI */ 
#delimit;
 global xvar "logNum DisLDPVS DISmun_ceif DISmun_needy_pc DISprimary_pc  DisLogPop  DisLogIncome malaportion DisPopDensity i.year"; 
#delimit ;
reg logFDismoneypc sdVotePop  $xvar if last==1 & year<=2000, vce(cluster HOR);
eststo d1b;  
 estadd local yearFE "Y";
estadd local disFE "N";
#delimit ;
reg logFDismoneypc sdVotePop  $xvar 
 if last==1   &year<1994 , vce(cluster HOR);
eststo d2b;
estadd local yearFE "Y";
estadd local disFE "N";

#delimit ;
xtreg logFDismoneypc sdVotePop  $xvar  if last==1 &year<=2000 ,fe i(HOR) vce(cluster HOR);
eststo d1bfe;  
estadd local yearFE "Y";
estadd local disFE "Y";

#delimit ;
xtreg logFDismoneypc sdVotePop  $xvar  if last==1 &year<1994  ,
 fe i(HOR) vce(cluster HOR);
eststo d2bfe;  
estadd local yearFE "Y";
estadd local disFE "Y";
 #delimit;	

esttab d1b d1bfe d2b d2bfe using "logFmoneypcDistrictLevel_SDpop.tex",replace  ar2 r2 se 
   title("Log(Transfers pc) to districts are regressed on support for the LDP (Models 1-4) and 
   the SD voting population  in HOR elections. Districts comprised of 
   municipalities of asymmetric sizes get more transfers. Models 1 and 2 consider the seven election (1980-2000). 
   Models 3 and 4 consider elections prior to refer (pre-1994)." )
   stats( N yearFE disFE r2,
	labels("Observations" "Year FE" "District FE" "R-squared")fmt(0 0 0 2))
 varlabel($labDIS $districtLab) order(HI logngaid_pc logNum DisLDPVS DISmun_ceif  DISmun_needy_pc DISprimary_pc  DisLogPop  DisLogIncome  DisPopDensity malaportion)
 drop(*.year  _cons) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers); 
 

 
 
 
 /* scatter plot to show the relationship between agriculture and HI */
 scatter HI DISprim   if last==1 &year<2000 &DisPopDensity <2 ,xtitle("Primary Production") ytitle("HI") 
 t1("District Level Primary Productivity and District Heterogenity");

 #delimit;
 scatter HI DisPopDensity  if last==1 &year<2000 &DisPopDensity <2 ,xtitle("Primary Production") ytitle("HI") 
 t1("District Level Primary Productivity and District Heterogenity");
 
 /********************** Muni level analysis *************************/ 
 /**********************************************************************/
/********************* Within District Analyses ***********************/
/**********************************************************************/	


/* Note that the rank function breaks down if there is only one muni in a district.*/  
#delimit;
 global controls  " mun_ceif needy_pc  primary_pc  lnpop  logincome_pc mun_population_density";
 #delimit;	
xtreg F1logngaid_pc bestLDP_VshareVP logngaid_pc $controls  if year<=2000  , fe i(year_d) vce(cluster year_d);
eststo m1;
estadd local disY "Y";
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP logngaid_pc $controls  if year<=2000  , fe i(year_d) vce(cluster year_d);
eststo m2;
estadd local disY "Y";
#delimit;	
xtreg F1logngaid_pc bestLDP_VshareVP logngaid_pc $controls  if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m3;
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP logngaid_pc $controls  if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m4;
 #delimit;
xtreg F1logngaid_pc bestLDP_VshareVP logngaid_pc $controls i.year_d  if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m5;
estadd local disY "Y";
estadd local munFE "Y";
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP logngaid_pc $controls i.year_d if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m6;estadd local disY "Y";
estadd local munFE "Y";
xtreg F1logngaid_pc bestLDP_VshareVP Llogngaid_pc $controls i.year_d  if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m7;estadd local disY "Y";
estadd local munFE "Y";
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP Llogngaid_pc $controls i.year_d if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m8;estadd local disY "Y";
estadd local munFE "Y";


/* include both lags in response to referee query */
 #delimit;
xtreg F1logngaid_pc bestLDP_VshareVP logngaid_pc Llogngaid_pc $controls i.year_d  if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m7a;estadd local disY "Y";
estadd local munFE "Y";
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP logngaid_pc Llogngaid_pc $controls i.year_d if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m8a;estadd local disY "Y";
estadd local munFE "Y";


#delimit;
esttab m1 m2 using "tempyeardistrictFE.tex",replace  ar2 r2 se 
   title("District year FE only ") 
 varlabel($lab) order(bestLDP_VshareVP rbestLDP_VshareVP) ; 
 

 
 
#delimit;
esttab m3 m4 m5 m6 using "basicVSwithFE.tex",replace  ar2 r2 se 
   title("Muni FE for all, district-year FE in latter models ") 
 varlabel($lab) order(bestLDP_VshareVP rbestLDP_VshareVP logngaid_pc Llogngaid_pc) drop(*.year_district);
#delimit; 
/*************************************************/
/* Table 1 */
/*************************************************/
esttab  m5 m6 m7 m8 using "table1.tex",replace  ar2 r2 se 
   title("Log(Transfers pc) after the year after an election are regressed on the level of 
   electoral support the municipality provided 
   the LDP (Models 1 and 3) and the municipality’s position in the ranking of 
   municipalities in its district in HOR elections (Models 2 and 4). 
   On average, increases in support lead to more transfers, 
   with the difference in amount of transfers received increasing at higher ranks. \label{tab:muni}
   Dependent Variable: \$Log(Transfers_{t+1})\$") 
   stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 varlabel($lab) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(bestLDP_VshareVP rbestLDP_VshareVP logngaid_pc Llogngaid_pc) drop(*.year_district _cons);

 #delimit;
esttab  m7a m8a using "basicVSwithFE2_2lags.tex",replace  ar2 r2 se 
   title("Log(Transfers pc) after the year after an election are regressed on the level of 
   electoral support the municipality provided 
   the LDP (Models 1 and 3) and the municipality’s position in the ranking of 
   municipalities in its district in HOR elections (Models 2 and 4). 
   On average, increases in support lead to more transfers, 
   with the difference in amount of transfers received increasing at higher ranks. Inclusion of measure of prior 
   transfer is years t nd t-1.    Dependent Variable: \$Log(Transfers_{t+1})\$")  
   stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 varlabel($lab) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(bestLDP_VshareVP rbestLDP_VshareVP logngaid_pc Llogngaid_pc) drop(*.year_district _cons);
 
 /* for appendix, repeat table 1 with the sum variable */ 
 /* Note that the rank function breaks down if there is only one muni in a district.*/  
#delimit;
 global controls  " mun_ceif needy_pc  primary_pc  lnpop  logincome_pc mun_population_density";
 
 #delimit;
xtreg F1logngaid_pc sumLDP_VshareVP logngaid_pc $controls i.year_d  if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m5s;
estadd local disY "Y";
estadd local munFE "Y";
 #delimit;	
xtreg F1logngaid_pc rsumLDP_VshareVP logngaid_pc $controls i.year_d if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m6s;estadd local disY "Y";
estadd local munFE "Y";
xtreg F1logngaid_pc sumLDP_VshareVP Llogngaid_pc $controls i.year_d  if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m7s;estadd local disY "Y";
estadd local munFE "Y";
 #delimit;	
xtreg F1logngaid_pc rsumLDP_VshareVP Llogngaid_pc $controls i.year_d if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m8s;estadd local disY "Y";
estadd local munFE "Y";
 #delimit;
 /**********************************************/
 /* Table A2 */
 /**********************************************/
esttab  m5s m6s m7s m8s using "tableA2.tex",replace  ar2 r2 se 
   title("Log(Transfers pc) after the year after an election are regressed on the level of 
   electoral support the municipality provided 
   the LDP (Models 1 and 3) and the municipality’s position in the ranking of 
   municipalities in its district in HOR elections (Models 2 and 4). 
   On average, increases in support lead to more transfers, 
   with the difference in amount of transfers received increasing at higher ranks.This is analogous to Table 1 in the paper, but with $\text{Sum LDP VS}_{m,t}$ and rank(Sum LDP VS$_{m,t})$. 
   \label{tab:sumversion}
   Dependent Variable: \$Log(Transfers_{t+1})\$") 
   stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 varlabel($lab) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(sumLDP_VshareVP rsumLDP_VshareVP logngaid_pc Llogngaid_pc) drop(*.year_district _cons);
 
 
/* Repeat above table with the inclusion of HighLDPVS*/ 
#delimit;
 global controlsH  "HighLDPVS mun_ceif needy_pc  primary_pc  lnpop  logincome_pc mun_population_density";
 #delimit;	
xtreg F1logngaid_pc bestLDP_VshareVP logngaid_pc $controlsH  if year<=2000  , fe i(year_d) vce(cluster year_d);
eststo m1b;
estadd local disY "Y";
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP logngaid_pc $controlsH  if year<=2000  , fe i(year_d) vce(cluster year_d);
eststo m2b;
estadd local disY "Y";
#delimit;	
xtreg F1logngaid_pc bestLDP_VshareVP logngaid_pc $controlsH  if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m3b;
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP logngaid_pc $controlsH  if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m4b;
 #delimit;
xtreg F1logngaid_pc bestLDP_VshareVP logngaid_pc $controlsH i.year_d  if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m5b;
estadd local disY "Y";
estadd local munFE "Y";
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP logngaid_pc $controlsH i.year_d if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m6b;estadd local disY "Y";
estadd local munFE "Y";
xtreg F1logngaid_pc bestLDP_VshareVP Llogngaid_pc $controlsH i.year_d  if year<=2000 , fe i(muncode_num) vce(cluster muncode_num);
eststo m7b;estadd local disY "Y";
estadd local munFE "Y";
 #delimit;	
xtreg F1logngaid_pc rbestLDP_VshareVP Llogngaid_pc $controlsH i.year_d if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo m8b;estadd local disY "Y";
estadd local munFE "Y";

#delimit;
/******************************************************************/
/* Table A12*/
/******************************************************************/
#delimit;
esttab  m5b m6b m7b m8b using "tableA12.tex",replace  ar2 r2 se 
   title("Robust check of Table 1. HighLDPVS is the municipality level support for the large vote getting LDP candidate in each district. Log(Transfers pc) after the year after an election are regressed on the level of     electoral support the municipality provided     the LDP (Models 1 and 3) and the municipality's position in the ranking of     municipalities in its district in HOR elections (Models 2 and 4).     On average, increases in support lead to more transfers,     with the difference in amount of transfers received increasing at higher ranks.  
   Dependent Variable: Log(Transfers$_{t+1}$)") 
   stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 varlabel($lab) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(bestLDP_VshareVP rbestLDP_VshareVP logngaid_pc Llogngaid_pc) drop(*.year_district _cons);
  
  

 
 #delimit;
 /* What are the effects of the controls varibales when there are no FE? */ 
reg F1logngaid_pc bestLDP_VshareVP logngaid_pc $controls   if year<=2000  , vce(cluster muncode_num);
eststo m5;
estadd local disY "N";
estadd local munFE "N";
 #delimit;	
reg F1logngaid_pc rbestLDP_VshareVP logngaid_pc $controls if year<=2000 ,  vce(cluster muncode_num);
eststo m6;estadd local disY "N";
estadd local munFE "N";
reg F1logngaid_pc bestLDP_VshareVP Llogngaid_pc $controls  if year<=2000 ,  vce(cluster muncode_num);
eststo m7;estadd local disY "N";
estadd local munFE "N";
 #delimit;	
reg F1logngaid_pc rbestLDP_VshareVP Llogngaid_pc $controls  if year<=2000  ,  vce(cluster muncode_num);
eststo m8;estadd local disY "N";
estadd local munFE "N";
esttab  m5 m6 m7 m8 using "NoFixedEffects.tex",replace  ar2 r2 se 
   title("NO FIXED EFFECTSTransfers are regressed on the level of electoral support the municipality provided 
   the LDP (Models 1 and 3) and the municipality’s position in the ranking of 
   municipalities in its district in HOR elections (Models 2 and 4). 
   On average, increases in support lead to more transfers, 
   with the difference in amount of transfers received increasing at higher ranks. 
   Dependent Variable: \$Log(Transfers_{t+1})\$") 
   stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 varlabel($lab) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(bestLDP_VshareVP rbestLDP_VshareVP logngaid_pc Llogngaid_pc) drop( _cons);

 
 
 
 
/**************************************************************/ 
 /* Placebo tests */ 
 /**************************************************************/ 
#delimit;
 global controls  " mun_ceif needy_pc  primary_pc  lnpop  logincome_pc mun_population_density";
/* Table A8:   BEST VS */ 
#delimit;	
quietly xtreg F1logngaid_pc bestLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_1 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc bestLDPp_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_2 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc bestSeniorLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_3 ;estadd local disY "Y";
estadd local munFE "Y";
#delimit;
quietly xtreg F1logngaid_pc bestLDP_VshareVP bestSeniorLDP_VshareVP  logngaid $controls i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_3a ;estadd local disY "Y";
estadd local munFE "Y";
#delimit;
quietly xtreg F1logngaid_pc bestnonLDP_VshareVP logngaid $controls  i.year_district if year<=2000 &ey==1, fe i(code)  vce(cluster code);
eststo t1m1_4 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc bestloseLDP_VshareVP  logngaid $controls i.year_district if year<=2000 &ey==1, fe i(code)  vce(cluster code);
eststo t1m1_5 ;estadd local disY "Y";
estadd local munFE "Y";

#delimit;
esttab t1m1_1 t1m1_2  t1m1_3 t1m1_3a t1m1_4 t1m1_5  using "tableA8.tex",replace drop(*.year_district _cons) ar2 r2 se 
 mtitles("LDP" "LDP+"  "SeniorLDP" "SeniorLDP" "NonLDP" "LosingLDP") 
stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 order(bestLDP_VshareVP bestLDPp_VshareVP bestSeniorLDP_VshareVP bestnonLDP_VshareVP bestloseLDP_VshareVP)
 title("Transfers received by a municipality the year after an election is 
 regressed on the level of electoral support the municipality provided, 
 where electoral support is defined as the largest vote share provided by the 
 municipality to various sets of LDP winners in the election at time $t$. 
 \label{tab:placeboBest}") 
 varlabels($lab)  ;
 
 
 /* Table A9:  Rank Best */ 
#delimit;	
quietly xtreg F1logngaid_pc rbestLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_1 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc rbestLDPp_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_2 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc rbestSeniorLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_3 ;estadd local disY "Y";
estadd local munFE "Y";
#delimit;
quietly xtreg F1logngaid_pc rbestLDP_VshareVP rbestSeniorLDP_VshareVP  logngaid $controls i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_3a ;estadd local disY "Y";
estadd local munFE "Y";
#delimit;
quietly xtreg F1logngaid_pc rbestnonLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_4 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc rbestloseLDP_VshareVP  logngaid $controls i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_5 ;estadd local disY "Y";
estadd local munFE "Y";

#delimit;
esttab t1m1_1 t1m1_2  t1m1_3 t1m1_3a t1m1_4 t1m1_5  using "tableA9.tex",replace drop(*.year_district _cons) ar2 r2 se 
 mtitles("LDP" "LDP+"  "SeniorLDP" "SeniorLDP" "NonLDP" "LosingLDP") 
stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 order(rbestLDP_VshareVP rbestLDPp_VshareVP rbestSeniorLDP_VshareVP rbestnonLDP_VshareVP rbestloseLDP_VshareVP)
 title("Transfers received by a municipality the year after an election is 
 regressed on the level of electoral support the municipality provided, 
 where electoral support is defined as the largest vote share provided by the 
 municipality to various sets of LDP winners in the election at time $t$. 
 \label{tab:placeboRankBest}") 
 varlabels($lab)  ;
 
 /* Table A10:   SUM */ 
#delimit;	
quietly xtreg F1logngaid_pc sumLDP_VshareVP logngaid $controls  i.year_district if year<=2000 &ey==1, fe i(code)  vce(cluster code);
eststo t1m1_1 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc sumLDPp_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_2 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc sumSeniorLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_3 ;estadd local disY "Y";
estadd local munFE "Y";
#delimit;
quietly xtreg F1logngaid_pc sumLDP_VshareVP sumSeniorLDP_VshareVP  logngaid $controls i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_3a ;estadd local disY "Y";
estadd local munFE "Y";
#delimit;
quietly xtreg F1logngaid_pc sumnonLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_4 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc sumloseLDP_VshareVP  logngaid $controls i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_5 ;estadd local disY "Y";
estadd local munFE "Y";

#delimit;
esttab t1m1_1 t1m1_2  t1m1_3 t1m1_3a t1m1_4 t1m1_5  using "tableA10.tex",replace drop(*.year_district _cons) ar2 r2 se 
 mtitles("LDP" "LDP+"  "SeniorLDP" "SeniorLDP" "NonLDP" "LosingLDP") 
stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 order(sumLDP_VshareVP sumLDPp_VshareVP sumSeniorLDP_VshareVP sumnonLDP_VshareVP sumloseLDP_VshareVP)
 title("Transfers received by a municipality the year after an election is 
 regressed on the level of electoral support the municipality provided, 
 where electoral support is defined as the largest vote share provided by the 
 municipality to various sets of LDP winners in the election at time $t$. 
\label{tab:placeboSum}") 
 varlabels($lab)  ;
 
 /* Table A11:   Rank SUM */ 
#delimit;	
quietly xtreg F1logngaid_pc rsumLDP_VshareVP logngaid $controls  i.year_district if year<=2000 &ey==1, fe i(code)  vce(cluster code);
eststo t1m1_1 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc rsumLDPp_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_2 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc rsumSeniorLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_3 ;estadd local disY "Y";
estadd local munFE "Y";
#delimit;
quietly xtreg F1logngaid_pc rsumLDP_VshareVP rsumSeniorLDP_VshareVP  logngaid $controls i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_3a ;estadd local disY "Y";
estadd local munFE "Y";
#delimit;
quietly xtreg F1logngaid_pc rsumnonLDP_VshareVP logngaid $controls  i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_4 ;estadd local disY "Y";
estadd local munFE "Y";
quietly xtreg F1logngaid_pc rsumloseLDP_VshareVP  logngaid $controls i.year_district if year<=2000&ey==1, fe i(code)  vce(cluster code);
eststo t1m1_5 ;estadd local disY "Y";
estadd local munFE "Y";

#delimit;
esttab t1m1_1 t1m1_2  t1m1_3 t1m1_3a t1m1_4 t1m1_5  using "tableA11.tex",replace drop(*.year_district _cons) ar2 r2 se 
 mtitles("LDP" "LDP+"  "SeniorLDP" "SeniorLDP" "NonLDP" "LosingLDP") 
 stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 order(rsumLDP_VshareVP rsumLDPp_VshareVP rsumSeniorLDP_VshareVP rsumnonLDP_VshareVP rsumloseLDP_VshareVP)
 title("Transfers received by a municipality the year after an election is 
 regressed on the level of electoral support the municipality provided, 
 where electoral support is defined as the largest vote share provided by the 
 municipality to various sets of LDP winners in the election at time $t$. \label{tab:placeboRankSum}") 
 varlabels($lab)  ;
 
 
 /****************************************************************************************/
  
 /* Support for Winning LDP Candidates */
 /* There is a big discussion in Japanese politics about rural/urban divide: show that heterogeneity is important too */ 
 
#delimit; 
reg bestLDP_VshareVP c.HI  c.mun_population_density lnpop mun_ceif needy_pc  primary_pc    
 logincome_pc  logNum  DISmun_ceif DISmun_needy_pc   DisLogPop DisLogIncome DisPopDensity malaportion  i.year if year<=2000  , vce(cluster code);
 eststo Wldp1; estadd local yearFE "Y";
  capture gen sample=e(sample);
 #delimit; 
reg bestLDP_VshareVP  c.mun_population_density lnpop mun_ceif needy_pc  primary_pc    
 logincome_pc  logNum  DISmun_ceif DISmun_needy_pc   DisLogPop DisLogIncome DisPopDensity malaportion  i.year if year<=2000 &sample==1 , vce(cluster code);
 eststo Wldp2; estadd local yearFE "Y";
 
sum mun_population_density primary_pc  lnpop if e(sample)==1 ;

 
 #delimit; 
reg bestLDP_VshareVP c.HI Llogngaid  c.mun_population_density lnpop mun_ceif needy_pc  primary_pc    
 logincome_pc  logNum  DISmun_ceif DISmun_needy_pc   DisLogPop DisLogIncome DisPopDensity malaportion  i.year if year<=2000  , vce(cluster code);
 eststo Wldp3; estadd local yearFE "Y";
  #delimit; 
capture  gen HILlogngaid=HI * Llogngaid ;
reg bestLDP_VshareVP HI Llogngaid HILlogngaid  mun_population_density lnpop mun_ceif needy_pc  primary_pc    
 logincome_pc  logNum  DISmun_ceif DISmun_needy_pc   DisLogPop DisLogIncome DisPopDensity malaportion  i.year if year<=2000  , vce(cluster code);
 eststo Wldp4; estadd local yearFE "Y";
 
#delimit; 
reg sumLDP_VshareVP c.HI  c.mun_population_density lnpop mun_ceif needy_pc  primary_pc    
 logincome_pc  logNum  DISmun_ceif DISmun_needy_pc   DisLogPop DisLogIncome DisPopDensity malaportion  i.year if year<=2000  , vce(cluster code);
 eststo Wldp1sum; estadd local yearFE "Y";
  capture gen sample=e(sample);
 #delimit; 
reg sumLDP_VshareVP  c.mun_population_density lnpop mun_ceif needy_pc  primary_pc    
 logincome_pc  logNum  DISmun_ceif DISmun_needy_pc   DisLogPop DisLogIncome DisPopDensity malaportion  i.year if year<=2000 &sample==1 , vce(cluster code);
 eststo Wldp2sum; estadd local yearFE "Y";
 #delimit; 
reg sumLDP_VshareVP c.HI Llogngaid  c.mun_population_density lnpop mun_ceif needy_pc  primary_pc    
 logincome_pc  logNum  DISmun_ceif DISmun_needy_pc   DisLogPop DisLogIncome DisPopDensity malaportion  i.year if year<=2000  , vce(cluster code);
 eststo Wldp3sum; estadd local yearFE "Y";
  #delimit; 
capture  gen HILlogngaid=HI * Llogngaid ;
reg sumLDP_VshareVP HI Llogngaid HILlogngaid  mun_population_density lnpop mun_ceif needy_pc  primary_pc    
 logincome_pc  logNum  DISmun_ceif DISmun_needy_pc   DisLogPop DisLogIncome DisPopDensity malaportion  i.year if year<=2000  , vce(cluster code);
 eststo Wldp4sum; estadd local yearFE "Y"; 
 
 
 
  drop HILlogngaid;
 
 drop sample;
 #delimit;
esttab Wldp2 Wldp1 Wldp3 Wldp4  using "LDPsupport.tex",replace drop(*.year _cons) ar2 r2 se  
 stats( N yearFE r2,
	labels("Observations" "Year FE"  "R-squared")fmt(0  0 2))
 order(HI mun_population_density lnpop primary_pc Llogngaid_pc HILlogngaid  )
 title("Support for winning LDP candidates (Best). \label{tab:LDPsupport}") 
 varlabels($lab $labDIS $districtLab  )  ;
 
 #delimit;
esttab Wldp2sum Wldp1sum Wldp3sum Wldp4sum  using "LDPsupportsum.tex",replace drop(*.year _cons) ar2 r2 se  
 stats( N yearFE r2,
	labels("Observations" "Year FE"  "R-squared")fmt(0  0 2))
 order(HI mun_population_density lnpop primary_pc Llogngaid_pc HILlogngaid  )
 title("Support for winning LDP candidates (Sum) \label{tab:LDPsupportsum}") 
 varlabels($lab $labDIS $districtLab )  ;
 
  #delimit;
  /**************************************************************/ 
 /* Table A7 */ 
 /**************************************************************/ 
esttab Wldp2 Wldp1 Wldp2sum Wldp1sum  using "tableA7.tex",replace drop(*.year _cons) ar2 r2 se  
 stats( N yearFE r2,
	labels("Observations" "Year FE"  "R-squared")fmt(0  0 2))
 order(HI mun_population_density lnpop primary_pc Llogngaid_pc HILlogngaid  )
 title("Support for winning LDP candidates as a function of  \label{tab:LDPsupport}") 
 varlabels($lab $labDIS $districtLab )  ;
 
  save temp2,replace;
/**************************************************************/
/*** Test Convexity ******/
/**************************************************************/
  #delimit;
 gen Ftransfer=100*F1ngaid_pc; /* value of pc transfers after election (*100 to scale better) */ 
  #delimit;
global outf "";
/* make cubic variables */
#delimit;
gen r2bestLDP_VshareVP=rbestLDP_VshareVP^2;
gen r3bestLDP_VshareVP=rbestLDP_VshareVP^3;
gen best2LDP_VshareVP=bestLDP_VshareVP^2;
gen best3LDP_VshareVP=bestLDP_VshareVP^3;
gen r2sumLDP_VshareVP=rsumLDP_VshareVP^2;
gen r3sumLDP_VshareVP=rsumLDP_VshareVP^3;
gen HIrbestLDP_VshareVP=rbestLDP_VshareVP*HI;
gen HIrbestLDP_VshareVP2=rbestLDP_VshareVP^2*HI;
 gen HIrbestLDP_VshareVP3=rbestLDP_VshareVP^3*HI;

gen HIbestLDP_VshareVP=bestLDP_VshareVP*HI;
gen HIbestLDP_VshareVP2=bestLDP_VshareVP^2*HI;
gen HIbestLDP_VshareVP3=bestLDP_VshareVP^3*HI;

gen HIrsumLDP_VshareVP=rsumLDP_VshareVP*HI;
gen HIrsumLDP_VshareVP2=rsumLDP_VshareVP^2*HI;
gen HIrsumLDP_VshareVP3=rsumLDP_VshareVP^3*HI;

keep if bestLDP_VshareVP~=.;
 global controls  " mun_ceif needy_pc  primary_pc  lnpop  logincome_pc mun_population_density";
 global xvar "logNum DisLDPVS DISmun_ceif DISmun_needy_pc DISprimary_pc  DisLogPop  
 DisLogIncome malaportion DisPopDensity ";

global labcube " r2bestLDP_VshareVP "Rank(Best LDP VS)\~^2\~"  r3bestLDP_VshareVP "Rank(Best LDP VS)\~^3\~"
 best2LDP_VshareVP "RBest LDP VS\~^2\~" best3LDP_VshareVP "Best LDP VS\~^3\~"
 r2sumLDP_VshareVP "Rank(Sum LDP VS)\~^2\~" r3sumLDP_VshareVP "Rank(Sum LDP VS)\~^3\~"
  HIrbestLDP_VshareVP "HI*Rank(Best LDP VS)" HIrbestLDP_VshareVP2 "HI*Rank(Best LDP VS)\~^2\~" HIrbestLDP_VshareVP3 "HI*Rank(Best LDP VS)\~^3\~"  
 HIbestLDP_VshareVP "HI*(Best LDP VS)" HIbestLDP_VshareVP2 "HI*(Best LDP VS)\~^2\~" HIbestLDP_VshareVP3 "HI*(Best LDP VS)\~^3\~"  
 HIrsumLDP_VshareVP "HI*Rank(Sum LDP VS)" HIrsumLDP_VshareVP2 "HI*Rank(Sum LDP VS)\~^2\~" HIrsumLDP_VshareVP3 "HI*Rank(Sum LDP VS)\~^3\~"";
 

#delimit;
global labDIS "HI "Population Index. HI" DISmun_ceif "District: Fiscal Strength" num "No. Municipalities" DisWinLDPVS "$\text{District: Best LDP VS}_{m,t}$"
DisLDP "District: LDP winner" DISmun_needy_pc "District: Needy Pop."
 DISprimary_pc "District: Agriculture" DIS_urban "District: Urban" DisLogPop "District: Log(Pop)" 
 DisLogIncome "District: Log(Income)" malaportion "Malaportionment" DisPopDensity "District: Pop.Density" "; 

#delimit;
global lab " lnpop  "Log(Population)" Llogngaid_pc "Log(Transfers(t-1))" logngaid_pc "Log(Transfers(t))" F1logngaid_pc "Log(Transfers(t+1))" mun_ceif "Fiscal Strength" needy_pc "Needy Population" primary_pc "Agriculture"  logincome_pc  "Log(Income)" bestLDP_VshareVP "Best LDP VS"  
bestLDPp_VshareVP "Best LDP+ VS" bestloseLDP_VshareVP "Best Losing LDP VS"
 bestLDP_VshareVP "Best LDP VS"  bestSeniorLDP_VshareVP "Best Senior LDP VS" bestnonLDP_VshareVP "Best Non-LDP VS" 
 sumLDPp_VshareVP "Sum LDP+ VS" sumloseLDP_VshareVP "Sum Losing LDP VS"
 sumLDP_VshareVP "Sum LDP VS"  sumSeniorLDP_VshareVP "Sum Senior LDP VS" sumnonLDP_VshareVP "Sum Non-LDP VS" 
 rbestLDPp_VshareVP "Rank Best LDP+ VS"  rsumLDPp_VshareVP "Rank Sum LDP+ VS"
 rbestPropLDP "Rank Best LDP PS"  rsumPropLDP "Rank Sum LDP PS" 
 rF1logngaid_pc "$\text{Rank Transfers}_{t+1}$" logngaid_pc "$\text{Transfer}_{m,t-1}$" rbestLDP_VshareVP "Rank(Best LDP VS)" mun_population_density "Pop.Density" 
 rsumLDP_VshareVP "Rank(Sum LDP VS)"
 rbestSeniorLDP_VshareVP "Rank(Best Senior LDP VS)" rbestloseLDP_VshareVP "Rank(Best Losing LDP VS)"  rbestnonLDP_VshareVP "Rank(Best Non-LDP VS)" 
  rsumSeniorLDP_VshareVP "Rank(Sum Senior LDP VS)" rsumloseLDP_VshareVP "Rank(Sum Losing LDP VS)"  rsumnonLDP_VshareVP "Rank(Sum Non-LDP VS)" 
 ";
#delimit;
global lab2 " LFtransfer "Log(Transfer) after 1993" dHI "$\Delta$ HI" 
dbestLDP_VshareVP "$\Delta \text{Best LDP VS}$"
 dHIdbestLDP "$\Delta \text{Best LDP VS} *\Delta$ HI" 
 drbestLDP_VshareVP "$\Delta \text{Rank Best LDP VS}$"
  dHIdrbestLDP "$\Delta \text{Rank Best LDP VS} *\Delta$ HI"
  dsumLDP_VshareVP "$\Delta \text{Sum LDP VS}$"
 dHIdsumLDP "$\Delta \text{Sum LDP VS} *\Delta$ HI" 
 drsumLDP_VshareVP "$\Delta \text{Rank Sum LDP VS}$"
  dHIdrsumLDP "$\Delta \text{Rank Sum LDP VS} * \Delta$ HI"
  rbestLDP_VshareVP "$\text{Rank Best LDP VS}$"
  rsumLDP_VshareVP "$\text{Rank Sum LDP VS}$"";
  


/* lets fit cubics and quadratics  in support */ 
  
    #delimit; 
	/*********************** Figure 1 ********************/
	/*********************** Figure 1 ********************/
 
 xtreg Ftransfer c.rbestLDP_VshareVP##c.rbestLDP_VshareVP##c.rbestLDP_VshareVP $controls if year<2000, fe i(year_d) vce(cluster year_d);
margins, at(rbestLDP_VshareVP=(0(.1)1)  ); 
  #delimit;
   global n1 "FT_cubic_rbestVS_DY";
  marginsplot, xdimension(rbestLDP_VshareVP) title("Per Capita Transfers and Rank(Best LDP VS)")  
     ytitle("Transfers (pc)")  
	 xtitle("Rank(Best LDP VS)")  recast(connected) 
  name($n1 , replace)    ; 
    graph export  "convex.pdf",replace;
  

#delimit;
xtreg Ftransfer rbestLDP_VshareVP r2bestLDP_VshareVP  $controls if year<2000, fe i(year_d) vce(cluster year_d);  
scalar llquad=e(ll); 
xtreg Ftransfer rbestLDP_VshareVP r2bestLDP_VshareVP r3bestLDP_VshareVP $controls if year<2000, fe i(year_d) vce(cluster year_d);
eststo m1; estadd local disY "Y";
estadd local munFE "N";test rbestLDP_VshareVP r2bestLDP_VshareVP r3bestLDP_VshareVP; estadd scalar JHT=r(p);
scalar llcube=e(ll);
estadd scalar cc=chi2(1, 2*(llcube-llquad));
xtreg Ftransfer rbestLDP_VshareVP r2bestLDP_VshareVP  $controls if year<2000, fe i(year_d) vce(cluster year_d);
eststo q1; estadd local disY "Y";
estadd local munFE "N";test rbestLDP_VshareVP r2bestLDP_VshareVP ; estadd scalar JHT=r(p);
scalar llcube=e(ll);
estadd scalar cc=chi2(1, 2*(llcube-llquad));


 /* pre 1994 reform */ 
xtreg Ftransfer c.rbestLDP_VshareVP##c.rbestLDP_VshareVP##c.rbestLDP_VshareVP $controls if year<1994, fe i(year_d) vce(cluster year_d);
margins, at(rbestLDP_VshareVP=(0(.1)1)  ); 
  #delimit;
   global n2 "FT_cubic_rbestVS_DY1994";
  marginsplot, xdimension(rbestLDP_VshareVP) title("Transfers - Cubic VS")  
     ytitle("Transfers (pc)")  
	 xtitle(Vote Share: LDP winners)  recast(connected) note("year_d fe, rbestVS")
  name($n2 , replace)    ; 
  graph export  "${n2}.pdf",replace;
  #delimit;
 xtreg Ftransfer c.rbestLDP_VshareVP##c.rbestLDP_VshareVP $controls if year<1994, fe i(year_d) vce(cluster year_d);
margins, at(rbestLDP_VshareVP=(0(.1)1)  ); 
  #delimit;
   global n2 "FT_cubic_rbestVS_DY1994";
  marginsplot, xdimension(rbestLDP_VshareVP) title("Transfers - Quadratic VS")  
     ytitle("Transfers (pc)")  
	 xtitle(Vote Share: LDP winners)  recast(connected) note("year_d fe, rbestVS")
  name(q$n2 , replace)    ; 
  graph export  "${n2}.pdf",replace; 
  

  xtreg Ftransfer rbestLDP_VshareVP r2bestLDP_VshareVP  $controls if year<1994, fe i(year_d) vce(cluster year_d);
  scalar llquad=e(ll); 
xtreg Ftransfer rbestLDP_VshareVP r2bestLDP_VshareVP r3bestLDP_VshareVP $controls if year<1994, fe i(year_d) vce(cluster year_d);
eststo m2; estadd local disY "Y";
estadd local munFE "N";test rbestLDP_VshareVP r2bestLDP_VshareVP r3bestLDP_VshareVP; estadd scalar JHT=r(p);
scalar llcube=e(ll);
estadd scalar cc=chi2(1, 2*(llcube-llquad));
xtreg Ftransfer rbestLDP_VshareVP r2bestLDP_VshareVP  $controls if year<1994, fe i(year_d) vce(cluster year_d);
  scalar llquad=e(ll); 
xtreg Ftransfer rbestLDP_VshareVP r2bestLDP_VshareVP  $controls if year<1994, fe i(year_d) vce(cluster year_d);
eststo q2; estadd local disY "Y";
estadd local munFE "N";test rbestLDP_VshareVP r2bestLDP_VshareVP ; estadd scalar JHT=r(p);
estadd scalar cc=chi2(1, 2*(llcube-llquad));

/* Best - not ranked */
xtreg Ftransfer c.bestLDP_VshareVP##c.bestLDP_VshareVP##c.bestLDP_VshareVP $controls if year<2000, fe i(year_d) vce(cluster year_d);
margins, at(bestLDP_VshareVP=(0(.1)1)  ); 



   global n3 "FT_cubic_bestVS_DY";
  marginsplot, xdimension(bestLDP_VshareVP) title("Transfers - Cubic VS")  
     ytitle("Transfers (pc)")  
	 xtitle(Vote Share: LDP winners)  recast(connected) note("year_d fe, bestVS")
  name($n1 , replace)    ; 
  graph export  "${n3}.pdf",replace;
  
  xtreg Ftransfer bestLDP_VshareVP best2LDP_VshareVP  $controls if year<2000, fe i(year_d) vce(cluster year_d);
  scalar llquad=e(ll); 
xtreg Ftransfer bestLDP_VshareVP best2LDP_VshareVP best3LDP_VshareVP $controls if year<2000, fe i(year_d) vce(cluster year_d);
eststo m3; estadd local disY "Y";estadd local munFE "N"; test bestLDP_VshareVP best2LDP_VshareVP best3LDP_VshareVP; estadd scalar JHT=r(p);
scalar llcube=e(ll);
estadd scalar cc=chi2(1, 2*(llcube-llquad));
/*rSUM */  #delimit;
xtreg Ftransfer c.rsumLDP_VshareVP##c.rsumLDP_VshareVP##c.rsumLDP_VshareVP $controls if year<2000, fe i(year_d) vce(cluster year_d);
margins, at(rsumLDP_VshareVP=(0(.1)1)  ); 
  #delimit;
   global n4 "FT_cubic_rsumVS_DY";
  marginsplot, xdimension(rsumLDP_VshareVP) title("Transfers - Cubic VS")  
     ytitle("Transfers (pc)")  
	 xtitle(Vote Share: LDP winners)  recast(connected) note("year_d fe, rsumVS")
  name($n4 , replace)    ; 
  graph export  "${n4}.pdf",replace;
  

  
  #delimit;
xtreg Ftransfer rsumLDP_VshareVP r2sumLDP_VshareVP  $controls if year<2000, fe i(year_d) vce(cluster year_d);  
    scalar llquad=e(ll); 
xtreg Ftransfer rsumLDP_VshareVP r2sumLDP_VshareVP r3sumLDP_VshareVP $controls if year<2000, fe i(year_d) vce(cluster year_d);
eststo m4; estadd local disY "Y";
estadd local munFE "N";test rsumLDP_VshareVP r2sumLDP_VshareVP r3sumLDP_VshareVP; estadd scalar JHT=r(p);
#delimit;scalar llcube=e(ll);
estadd scalar cc=chi2(1, 2*(llcube-llquad));

  /************************************************************/
  /******************Table A5 ********************/
  /************************************************************/
  
  
 #delimit;
esttab  m1  m2 m3 m4 using "tableA5.tex",replace  ar2 r2 se 
   title("Convexity: DV=pc Transfer at t+1") 
   stats( N disY munFE JHT r2 ,
	labels("Observations" "District-Year FE" "Municipality FE" "Joint Hypothesis Test" "R-squared" )fmt(0 0 0  4 4 3))
 varlabel($lab $labcube) mlabels("Rank(Best LDP VS)" "Pre-1994 reform" "Best LDP VS" "Rank(Sum LDP VS)", nonumbers) 
 order(rbestLDP_VshareVP r2bestLDP_VshareVP r3bestLDP_VshareVP
 bestLDP_VshareVP best2LDP_VshareVP best3LDP_VshareVP  rsumLDP_VshareVP r2sumLDP_VshareVP r3sumLDP_VshareVP) 
  drop( _cons) substitute(\~ $);  
  

/***************************** Look at differences between elections **************************/
#delimit;
use temp2,clear ;
xtset code year;
replace ey=0 if ey==.;
set more off;
rename ncands_electoral_district ncand;
foreach i in  bestLDP_VshareVP sumLDP_VshareVP  rbestLDP_VshareVP rsumLDP_VshareVP 
logngaid_pc F1logngaid_pc rF1logngaid_pc  mun_ceif needy_pc  primary_pc
  lnpop  logincome_pc mun_population_density HI year_district num
  logNum  malaportion numLDPcand numLDPwin totseat_in_electoral_district ncand

{;
	display "`i'";
	capture drop temp; 
	
	gen temp=L.`i' if ey~=1;
	replace temp=L.temp if L.temp~=. &ey==0;
	
	gen LE`i'=L.temp if ey==1;
	gen D`i'=`i'-LE`i';
	drop temp;
	
	};
	

 #delimit;
 xtsum DbestLDP_VshareVP DsumLDP_VshareVP  DrbestLDP_VshareVP DrsumLDP_VshareVP 
DF1logngaid_pc if ey==1 &year<=1994; 
sum DbestLDP_VshareVP DsumLDP_VshareVP  DrbestLDP_VshareVP DrsumLDP_VshareVP 
DF1logngaid_pc if ey==1 &year<=1994;



 capture gen DHIsumLDP_VshareVP=DHI*sumLDP_VshareVP;
 capture gen DHIrsumLDP_VshareVP=DHI*rsumLDP_VshareVP;
  capture gen DHILEsumLDP_VshareVP=DHI*LEsumLDP_VshareVP;
 capture gen DHILErsumLDP_VshareVP=DHI*LErsumLDP_VshareVP;
  #delimit;
 global controls  " Dmun_ceif Dneedy_pc  Dprimary_pc  Dlnpop Dlogincome_pc   Dmun_population_density";
   global Ddistrict " Dmalaportion DnumLDPwin DnumLDPcand";

#delimit;
global lab2 " LFtransfer "Log(Transfer pc) after 1993 election" dHI "$\Delta$ HI" 
dbestLDP_VshareVP "$\Delta \text{Best LDP VS}$"
 dHIdbestLDP "$\Delta \text{Best LDP VS} *\Delta$ HI" 
 drbestLDP_VshareVP "$\Delta \text{Rank Best LDP VS}$"
  dHIdrbestLDP "$\Delta \text{Rank Best LDP VS} *\Delta$ HI"
  dsumLDP_VshareVP "$\Delta \text{Sum LDP VS}$"
 dHIdsumLDP "$\Delta \text{Sum LDP VS} *\Delta$ HI" 
 drsumLDP_VshareVP "$\Delta \text{Rank Sum LDP VS}$"
  dHIdrsumLDP "$\Delta \text{Rank Sum LDP VS} * \Delta$ HI"
  rbestLDP_VshareVP "$\text{Rank Best LDP VS}$"
  bestLDP_VshareVP "Best LDP VS"
  rsumLDP_VshareVP "$\text{Rank Sum LDP VS}$"
  dmal "$\Delta \$ Malaportionment" 
  logngaid_pc "Log(Transfer) 1996"  sumLDP_VshareVP "Sum LDP VS"
  rsumLDP_VshareVP "Rank(Sum LDP VS)"
  dmal "\$Delta \$ malapportion"
  dmaldsumLDP "$\Delta\$  Sum LDP VS * \$ Delta \$ malapportion"
  dmaldrsumLDP "$\Delta\$ Rank Sum LDP VS * \$ Delta \$ malapportion" 
  ";
  
    
#delimit;
global deltalab "   Dmun_ceif  "$\Delta$ Fical Strength" Dneedy_pc "$\Delta$ Needy Population" Dprimary_pc "$\Delta$ Agriculture"
 Dlnpop "$\Delta$ Ln(Population)"   Dlogincome_pc "$\Delta$ Ln(Income)" 
  Dnum "$\Delta$ Number Muni" DnumLDPwin "$\Delta$ Number LDP winner" dnumLDPcand "$\Delta$ Num LDP candidates"
  DsumLDP "$\Delta$Sum LDP VS"  DbestLDP_VshareVP "$\Delta$ Best LDP VS"  DrbestLDP_VshareVP "$\Delta$ Rank(Best LDP VS)"
  Dmun_population_density "$\Delta$ Pop. Density" DHI "$\Delta$ HI" DlogNum "$\Delta$ Log(Num. Municipalities)"
  Dmalaportion "$\Delta$ malapportion" numLDPcand "$\Delta$ malapportion" numLDPwin  "$\Delta$ LDP Winning Candidates" 
  DnumLDPcand "$\Delta$ LDP Candidates"       
   DsumLDP_VshareVP "$\Delta \text{Sum LDP VS}$" 
 DrsumLDP_VshareVP "$\Delta \text{Rank( Sum LDP VS)}$" Dncand "$\Delta$ number of Candidates" "; 
   
   
#delimit;
/* All elections */

xtreg DF1logngaid_pc DbestLDP_VshareVP DHI $controls $Ddistrict i.year_d   if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff1a;
estadd local disY "Y";
estadd local munFE "Y";
  #delimit;
xtreg DF1logngaid_pc DrbestLDP_VshareVP DHI $controls $Ddistrict i.year_d   if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff2a;
estadd local disY "Y";
estadd local munFE "Y";
#delimit;
xtreg DF1logngaid_pc DsumLDP_VshareVP DHI $controls $Ddistrict i.year_d   if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff3a;
estadd local disY "Y";
estadd local munFE "Y";
  #delimit;
xtreg DF1logngaid_pc DrsumLDP_VshareVP DHI $controls $Ddistrict  i.year_d   if year<=2000  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff4a;
estadd local disY "Y";
estadd local munFE "Y";
#delimit;
 /*****************************************************/
 /****** Table A3 ********/
  /*****************************************************/
esttab diff1a diff2a diff3a diff4a using "tableA3.tex",replace  ar2 r2 se
      title("Change in Log Transfers across elections as a function of change in the level of support
	  for LDP winning candidates. 
   Dependent Variable: \$\Delta_{e_i}Log(Transfers_{t+1})\$ . 
   dis-year FE, muni FE \label{tab:T2allelections}") 
   stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 varlabel($deltalab ) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(DbestLDP_VshareVP DrbestLDP_VshareVP DsumLDP_VshareVP DrsumLDP_VshareVP) drop(*.year_district _cons);
   
   
/******* PRE-1993 *******/
  #delimit;

xtreg DF1logngaid_pc DbestLDP_VshareVP DHI $controls $Ddistrict i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff1apre;
estadd local disY "Y";
estadd local munFE "Y";
  #delimit;
xtreg DF1logngaid_pc DrbestLDP_VshareVP DHI $controls $Ddistrict i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff2apre;
estadd local disY "Y";
estadd local munFE "Y";
xtreg DF1logngaid_pc DsumLDP_VshareVP DHI $controls $Ddistrict i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff3apre;
estadd local disY "Y";
estadd local munFE "Y";
  #delimit;
xtreg DF1logngaid_pc DrsumLDP_VshareVP DHI $controls $Ddistrict  i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff4apre;
estadd local disY "Y";
estadd local munFE "Y";
#delimit;
 /*****************************************************/
 /****** Table 2 ********/
  /*****************************************************/
esttab diff1apre diff2apre diff3apre diff4apre using "table2.tex",replace  ar2 r2 se
      title("Change in Log Transfers across elections  
   Dependent Variable: \$\Delta_{e_i}Log(Transfers_{t+1})\$ elections before 1994. 
   dis-year FE, muni FE") 
   stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 varlabel($deltalab ) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(DbestLDP_VshareVP DrbestLDP_VshareVP DsumLDP_VshareVP DrsumLDP_VshareVP) drop(*.year_district _cons);



 /******* PRE-1993 with contemporary transfers *******/
  #delimit;

xtreg DF1logngaid_pc DbestLDP_VshareVP DHI  logngaid_pc $controls $Ddistrict i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff1apre;
estadd local disY "Y";
estadd local munFE "Y";
  #delimit;
xtreg DF1logngaid_pc DrbestLDP_VshareVP DHI  logngaid_pc $controls $Ddistrict i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff2apre;
estadd local disY "Y";
estadd local munFE "Y";
xtreg DF1logngaid_pc DsumLDP_VshareVP DHI  logngaid_pc $controls $Ddistrict i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff3apre;
estadd local disY "Y";
estadd local munFE "Y";
  #delimit;
xtreg DF1logngaid_pc DrsumLDP_VshareVP DHI  logngaid_pc $controls $Ddistrict  i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
eststo diff4apre;
estadd local disY "Y";
estadd local munFE "Y";
#delimit;
 /*****************************************************/
 /****** Table A4 ********/
  /*****************************************************/
 esttab diff1apre diff2apre diff3apre diff4apre using "tableA4.tex",replace  ar2 r2 se
      title("Change in Log Transfers across elections  
   Dependent Variable: \$\Delta_{e_i}Log(Transfers_{t+1})\$ elections before 1994. 
   dis-year FE, muni FE \label{tab:T2currentT}") 
   stats( N disY munFE r2,
	labels("Observations" "District-Year FE" "Municipality FE" "R-squared")fmt(0 0 0 2))
 varlabel($deltalab ) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(DbestLDP_VshareVP DrbestLDP_VshareVP DsumLDP_VshareVP DrsumLDP_VshareVP logngaid_pc) drop(*.year_district _cons);

 
 

#delimit;

/* Restrict analysis to municipalities that had very high levels of support for LDP at the prior election */
  /* A referee wondered whether the analysis might not apply to really high supportive muni. Lets look at only muni that were ranked highly in the 
 previous election  */
  /********* slippage of top supporters **********
 xtreg DF1logngaid_pc DbestLDP_VshareVP DHI $controls $Ddistrict i.year_d   if year<=1994  , fe i(muncode_num) vce(cluster muncode_num);
*/
/******************* What happens when key supporters slip in the ranking? ********************/


sort year hor_electoral_district;
#delimit;
foreach i in sumLDP_VshareVP  bestLDP_VshareVP{;
	display "`i'";
	capture drop temp; 
	capture drop rank rr;
	sort year_d `i' lnpop;
	egen rank = rank(`i') if `i'~=., by(year_d) ;
	by year_d: egen rr=max(rank);
	/*gen r`i'1 = (rank==rr);
	gen r`i'2 = (rank==rr-1);
	gen r`i'3 = (rank==rr-2);
	gen r`i'Last = (rank==1);*/
	gen R`i'=rr-rank+1;
	drop rr rank;
	};	
#delimit;

sort code year ;
xtset code year;
foreach i in  RsumLDP_VshareVP RbestLDP_VshareVP 
{;
	display "`i'";
	capture drop temp; 
	
	gen temp=L.`i' if ey~=1;
	replace temp=L.temp if L.temp~=. &ey==0;
	
	gen LE`i'=L.temp if ey==1;
	gen D`i'=`i'-LE`i';
	drop temp;
	
	};
	
	/*********************** Calculate the votes and VS between 1st and 2nd, 2nd and 3rd etc..... **********/


#delimit;
gen temp1=bestLDP_VshareVP if RbestLDP_VshareVP==1;
gen temp2=bestLDP_VshareVP if RbestLDP_VshareVP==2;
gen temp3=bestLDP_VshareVP if RbestLDP_VshareVP==3;
#delimit;
sort year hor_electoral_district;
by year hor_electoral_district:egen T1=max(temp1);
by year hor_electoral_district:egen T2=max(temp2);
by year hor_electoral_district:egen T3=max(temp3);
gen T12=(T1-T2)*mun_voting_pop;
gen T23=(T2-T3)*mun_voting_pop;
gen T13=(T1-T3)*mun_voting_pop;
sum T12 T23 if year==1989 &first==1 , detail ;/*first for one obs per district*/
#delimit;
sort code year ;
xtset code year;


 
 
reg DF1logngaid_pc DrsumLDP_VshareVP  logngaid_pc   i.year  
     if year<=1994& LERsumLDP_VshareVP<=2 & ey==1;
	 eststo slip1;
	 estadd local Yfe "Y";
	  tab RsumLDP_VshareVP if year<1994 & LERsumLDP_VshareVP<=2 & e(sample)==1;
	  #delimit; 
reg DF1logngaid_pc DrsumLDP_VshareVP logngaid_pc   mun_ceif needy_pc  primary_pc  lnpop  logincome_pc   i.year  
     if year<=1994& LERsumLDP_VshareVP<=2 ; 
	  eststo slip2;
	  estadd local Yfe "Y";
 
 #delimit;  
reg DF1logngaid_pc DrsumLDP_VshareVP  logngaid_pc $controls  i.year   if year<=1994& LERsumLDP_VshareVP<=2 & ey==1;


esttab slip1 slip2 using "tableA13.tex",replace  ar2 r2 se
      title("Change in Log Transfers across elections  
   Dependent Variable for the Top 2 ranked municipalities in the prior election: \$\Delta_{e_i}Log(Transfers_{t+1})\$ elections before 1994. 
    \label{tab:slipage}") 
   stats( N Yfe r2,
	labels("Observations" "Year FE" "R-squared")fmt(0 0 2))
 varlabel($deltalab ) mlabels("Model 1" "Model 2" "Model 3" "Model 4", nonumbers)
 order(DrsumLDP_VshareVP ) drop(*.year _cons) ;
 

 
/************* 1996-1993 Comparison ************/
 
   #delimit;
  global Ddistrict " Dmalaportion  Dncand";


/* Diagnostic: How much mixing occurs in terms of muni in new districts 
Many new distrcts are prefect subsets on the old district and hence all the district change variables are identical for all 
muni in new district and same FE nd lagged LE.*/   
#delimit;
capture drop NN DsdHI;
sort year_d;
keep if year==1996& DHI~=.;
by year_d: gen NN=_N;
keep if NN>1;
by year_d: egen DsdHI=sd(DHI);

by year_d: gen nn=_n;

sum DsdHI if nn==1& DsdHI~=0 ;
list DsdHI if nn==1 & DsdHI~=0;
  
corr DHI Dmalaportion  Dncand if year==1996 & DrsumLDP_VshareVP~=. &DsdHI>0 &DsdHI~=.;
sum DHI Dmalaportion  Dncand if year==1996 & DrsumLDP_VshareVP~=. &DsdHI>0 &DsdHI~=.;  
 #delimit;
 capture gen DHIsumLDP_VshareVP=DHI*sumLDP_VshareVP;
 capture gen DHIrsumLDP_VshareVP=DHI*rsumLDP_VshareVP;
  capture gen DHILEsumLDP_VshareVP=DHI*LEsumLDP_VshareVP;
 capture gen DHILErsumLDP_VshareVP=DHI*LErsumLDP_VshareVP;
 

 
   #delimit;
 xtreg F1logngaid_pc DHI DsumLDP_VshareVP logngaid_pc
  LEF1logngaid  $controls  i.LEyear_d   
 if year==1996 & DsdHI>0 &DsdHI~=. &DrsumLDP_VshareVP~=.
   , fe i(year_d) vce(cluster year_d);
eststo diff1d1996; 
estadd local dis93 "Y";
estadd local dis96 "Y";


xtreg F1logngaid_pc DHI DrsumLDP_VshareVP  LEF1logngaid  logngaid_pc
$controls  i.LEyear_d   if  year==1996 &DsdHI>0 &DsdHI~=.&DrsumLDP_VshareVP~=.
 , fe i(year_d) vce(cluster year_d);
eststo diff2d1996;
estadd local dis93 "Y";
estadd local dis96 "Y";

  #delimit;
 xtreg F1logngaid_pc DHI sumLDP_VshareVP logngaid_pc
 LEF1logngaid  $controls Dmalaportion i.LEyear_d   
 if year==1996 & DsdHI>0 &DsdHI~=. &DrsumLDP_VshareVP~=.
 , fe i(year_d) vce(cluster year_d);
eststo diff3d1996; 
estadd local dis93 "Y";
estadd local dis96 "Y";
  #delimit;

xtreg F1logngaid_pc DHI rsumLDP_VshareVP  LEF1logngaid  logngaid_pc
$controls Dmalaportion i.LEyear_d   if  year==1996 &DsdHI>0 &DsdHI~=. &DrsumLDP_VshareVP~=.
, fe i(year_d) vce(cluster year_d);
eststo diff4d1996;
estadd local dis93 "Y";
estadd local dis96 "Y";
 
 #delimit;
 xtreg F1logngaid_pc DHI DsumLDP_VshareVP logngaid_pc
 LEF1logngaid  $controls Dmalaportion   i.LEyear_d   
 if year==1996 & DsdHI>0 &DsdHI~=. &DrsumLDP_VshareVP~=.
 , fe i(year_d) vce(cluster year_d);
eststo diff5d1996; 
estadd local dis93 "Y";
estadd local dis96 "Y";
  #delimit;

xtreg F1logngaid_pc DHI DrsumLDP_VshareVP  LEF1logngaid  logngaid_pc
$controls Dmalaportion i.LEyear_d   if  year==1996 &DsdHI>0 &DsdHI~=. &DrsumLDP_VshareVP~=.
, fe i(year_d) vce(cluster year_d);
eststo diff6d1996;
estadd local dis93 "Y";
estadd local dis96 "Y";
 

 
 #delimit;
 /******************************************************/
 /****** Table 4 ***************************************/
 /******************************************************/
esttab  diff1d1996 diff2d1996 diff3d1996 diff4d1996  using
			"table4.tex",replace  ar2 r2 se
      title("Transfers after the 1996 election (Log(Transfer\$_{1997}\$
	  are regressed on change in a municipality's 
	  HI score between 1993 and 1996 and support for the LDP.  
	  Municipalities shuffled into districts where municipalities varied more in size 
	  received larger transfers, and this is especially the case for municipalities that 
	  also increased their support for the LDP. \label{tab:er}") 
   stats( N dis93 dis96 r2,
	labels("Observations" "District FE 1993" "District FE 1996" "R-squared")fmt(0 0 0 2))
 varlabel($deltalab $lab LEF1logngaid_pc "Log(Transfer(1994))") mlabels("Model 1" "Model 2" , nonumbers)
 order(  DHI  DsumLDP_VshareVP DrsumLDP_VshareVP sumLDP_VshareVP rsumLDP_VshareVP LEF1logngaid_pc logngaid_pc  $controls Dmalaportion  ) 
 drop(*.LEyear_district _cons);
 
 erase "temp2.dta";
 
 
